package cn.elves.model.auth.controller;

import cn.elves.model.auth.entity.dto.MenuDTO;
import cn.elves.model.auth.entity.po.Menu;
import cn.elves.model.auth.service.MenuService;
import cn.elves.model.entity.dto.response.ResponseDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("menu/page/{pageNum}/{pageSize}")
    public ResponseDTO<Page<Menu>> getMenuPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {

        return ResponseDTO.success(menuService.page(new Page<>(pageNum,pageSize)));
    }

    @GetMapping("menu/{id}")
    public ResponseDTO<Menu> getMenuDetail(@PathVariable("id") Long id) {
        return ResponseDTO.success(menuService.getById(id));
    }

    @PostMapping("menu")
    public ResponseDTO<Menu> saveMenu(@RequestBody MenuDTO MenuDTO) {
        menuService.save(MenuDTO.toMenu());
        return ResponseDTO.success();
    }

    @PutMapping("menu")
    public ResponseDTO<Menu> updateMenu(@RequestBody MenuDTO MenuDTO) {
        menuService.updateById(MenuDTO.toMenu());
        return ResponseDTO.success();
    }

    @DeleteMapping("menu/{id}")
    public ResponseDTO<Menu> deleteMenuById(@PathVariable("id") Long id) {
        menuService.removeById(id);
        return ResponseDTO.success();
    }

}
